<?php

namespace App\Http\Controllers\Api;

use App\Models\AgricGoods;
use App\Models\AgricGoodsCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AgricGoodsController extends Controller
{
    protected $pagesize = '';
    public function __construct(Request $request)
    {
        $this -> pagesize = $request -> input('pagesize') ?? '5';
    }



    // 获取某个类的商品列表
    public function getAgricGoods(Request $request)
    {
        $getAgricGoods = AgricGoods::getAgricGoods($request);

        return jsonSuccess($getAgricGoods);
    }


    // 获取商品分类
    public function getAgricCategory(Request $request)
    {
        $getAgricCategory = AgricGoodsCategory::select('id','category_name','category_sort','status') -> where('status',1) -> orderBy('category_sort') -> get();

        return jsonSuccess($getAgricCategory);
    }


    // 获取某个商品的详情
    public function details(Request $request)
    {

        if(!$request -> filled('id')){
            return jsonError(4020);
        }
        $id = $request -> input('id');

        // 商品的点击数
        AgricGoods::agricGoodsOperated($id,4);
        $details = AgricGoods::getDetails($id);

        if($details){
            return jsonSuccess($details);
        }else{
            return jsonError('',4008,404);
        }

    }



    // 针对商品感兴趣
    public function agricGoodsOperatedLogs(Request $request)
    {

        $goods_id  = $request -> input('goods_id');
        $type      = $request -> input('type');
        if($goods_id){


            $res = AgricGoods::agricGoodsOperated($goods_id, $type);
            if($res){
                return jsonSuccess();
            }else{
                return jsonError();
            }
        }else{
            return jsonError(4020);
        }
    }

}
